package com.example.tests;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import java.util.Random;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;

public class ContactRemovalTests extends TestBase{

	@Test(dataProvider = "randomValidContactsGenerator")
	public void deleteSomeContact(Object o) {
		
		SortedListOf<ContactData> oldList = app.getModel().getContacts();
		    
		Random rnd = new Random();
		int index =  rnd.nextInt(oldList.size()-1);
		
		app.getContactHelper().deleteContact(index);

		SortedListOf<ContactData> newList = app.getModel().getContacts();
	    
		assertThat(newList, equalTo(oldList.without(index)));
		
		if (wantToCheck()) {
			  if ("yes".equals(app.getProperty("check.db"))) {	 
				  
				  ContactData.assertDbContactsEqual(app.getModel().getContacts(), app.getHibernateHelper().listContacts());
				  System.out.println("DB verification passed");
			  }
			  if ("yes".equals(app.getProperty("check.ui"))) {

				  SortedListOf<ContactDataRow> mdContacts = ContactDataRow.buildFromContactList(app.getModel().getContacts()); 	  
				  SortedListOf<ContactDataRow> uiContacts = app.getContactHelper().getUIContacts();
				  
				  assertThat("UI verification failed", mdContacts, equalTo(uiContacts));
				  System.out.println("UI verification passed");
			  }
		}
		
	}
		
}
